<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class TaskPeer extends BaseTaskPeer {

	public static $resourceTypes = array(
		1 => 'general', 
		2 => 'note'
	);
	
	const NOTE_RESOURCE = 'note';
	
	const OPEN = 1;
	const COMPLETED = 2;

	public static function retrieveById($id) {
		return TaskPeer::retrieveByPK($id);
	}
	
	public static function retrieveByGroupId($group_id) {
		$c = new Criteria();
		$c->add(TaskPeer::GROUP_ID, $group_id);
		return TaskPeer::doSelect($c);
	}

	public static function retrieveByTaskListId($task_list_id) {
		$c = new Criteria();
		$c->add(TaskPeer::TASK_LIST_ID, $task_list_id);
		return TaskPeer::doSelect($c);
	}
	

	
	//retrieve task lists that are open and due before the given date
	public static function retrieveLateTasksByGroupIdAndUserId($group_id,$user_id, $date) {
		$c = new Criteria();
		$c->add(TaskPeer::GROUP_ID, $group_id);
		$c->add(TaskPeer::USER_ID, $user_id);
		$c->add(TaskPeer::DUE_DATE, $date, Criteria::LESS_THAN);
		$c->add(TaskPeer::STATUS, TaskPeer::OPEN);
		$c->addAscendingOrderByColumn(TaskPeer::DUE_DATE);
		
		return TaskPeer::doSelect($c);

	}
	
	//retrieve tasks that are open and due on the given date
	public static function retrieveTasksByDate($date) {
		
		//get averages for ratings
		$connection = Propel::getConnection();
		$query = 'SELECT *'.
		' FROM '.TaskPeer::TABLE_NAME.
		' WHERE '.TaskPeer::STATUS.' = '.TaskPeer::OPEN.' AND DATE('.TaskPeer::DUE_DATE.') = \''.$date.'\'';
		$statement = $connection->prepare($query);
		$statement->execute();
		
		$result= $statement->fetchAll();
		
		return $result;
	}

	//retrieve tasks that are open and past due on the given date
	public static function retrieveLateTasksByDate($date) {
		
		//get averages for ratings
		$connection = Propel::getConnection();
		$query = 'SELECT *'.
		' FROM '.TaskPeer::TABLE_NAME.
		' WHERE '.TaskPeer::STATUS.' = '.TaskPeer::OPEN.' AND DATE('.TaskPeer::DUE_DATE.') <= \''.$date.'\'';
		$statement = $connection->prepare($query);
		$statement->execute();
		
		$result= $statement->fetchAll();
		
		return $result;
	}
	
	
	//retrieve all tasks that are completed for a given group 
	public static function retrieveCompletedTasksByGroupId($group_id) {
		$c = new Criteria();
		$c->add(TaskPeer::GROUP_ID, $group_id);
		$c->add(TaskPeer::STATUS, TaskPeer::COMPLETED);
		$c->addDescendingOrderByColumn(TaskPeer::UPDATED_AT);

		return TaskPeer::doSelect($c);

	}
	
	//retrieve list of all open tasks that are not due before the given date
	public static function retrieveOpenTasksByUserIdAndGroupId($group_id, $user_id, $date) {
		$c = new Criteria();
		$c->add(TaskPeer::GROUP_ID, $group_id);
		$c->add(TaskPeer::DUE_DATE, $date, Criteria::GREATER_THAN);
		$c->add(TaskPeer::STATUS, TaskPeer::OPEN);
		$c->add(TaskPeer::USER_ID, $user_id);
		
		return TaskPeer::doSelect($c);

	}
	
	
	//retrieve by group id and the day it was created as a timestamp
	public static function retrieveByGroupIdAndCreatedAt($group_id, $time_stamp)
	{
		$c = new Criteria();
		$c->add(TaskPeer::GROUP_ID, $group_id);
		$c->add(TaskPeer::CREATED_AT, date('Y-m-d', $time_stamp), Criteria::GREATER_EQUAL);
		$c->addAnd(TaskPeer::CREATED_AT, date('Y-m-d', $time_stamp + 60*60*24), Criteria::LESS_THAN);
			
 		return TaskPeer::doSelect($c); 		
	
	}
	
}
